
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-29 下午2:38:09 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.topic.Topic;
import org.alipay4j.model.topic.TopicItem;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: 营销位操作组件
 * @Description: 营销位操作组件
 * @author heliang
 * @date 2019-8-29 下午2:38:09 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */
public class TopicComponent extends AbstractComponent {

    public TopicComponent(Alipay alipay) {
        super(alipay);
    }

    /**
    * @Title: 营销位添加接口 
    * @Description: 开发者可以通过此接口，在生活号主页中配置营销位，支持展示4-8个营销位，满足商家对业务的需求，需要注意的是，当某个营销位需要显示为头图时，必须要配置以下三个参数：营销位描述 (sub_title)、营销位图片（img_url）、营销位链接地址（link_url），如不需要展示头图时，以上三个参数均不要传。
    * @author heliang
    * @param title 营销位名称
    * @param sub_title 营销位描述。营销位需要展示头图时，必须填写该参数。
    * @param img_url 营销位图片url, 尺寸为996*450，最大不超过5M，支持格式:.jpg、.png ，请先调用 图片上传接口获得图片url。营销位需要展示头图时，必须填写该参数。
    * @param link_url 营销位跳转地址，点击营销位头图跳到的链接url。营销位需要展示头图时，必须填写该参数。
    * @param link_type 跳转类型，网页:HTTP、小程序:APP，不传默认HTTP
    * @param topic_items 营销位内容，数量限制：大于4个，小于8个   
    * @return boolean
    * @throws AlipayException 
     */
    public boolean create(String title,String sub_title,String img_url,
            String link_url,String link_type,List<TopicItem> topic_items) throws AlipayException {
        if (ObjectHelper.isEmpty(title)) {
            throw new IllegalArgumentException("title can't be null or empty");
        }
        if (ObjectHelper.isEmpty(topic_items)) {
            throw new IllegalArgumentException("topic_items can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("title", title);
        body.put("sub_title", sub_title);
        body.put("img_url", img_url);
        body.put("link_url", link_url);
        body.put("link_type", link_type);
        body.put("topic_items", JSONObject.toJSONString(topic_items));
        body.put("method", "alipay.open.public.topic.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_topic_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_topic_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    
    /**
    * @Title: 营销位修改接口 
    * @Description: 营销位修改接口
    * @author heliang
    * @param topic_id  营销位id
    * @param title 营销位名称
    * @param sub_title 营销位描述。营销位需要展示头图时，必须填写该参数
    * @param img_url 营销位图片url, 尺寸为996*450，最大不超过5M，支持格式:.jpg、.png ，请先调用 图片上传接口获得图片url。营销位需要展示头图时，必须填写该参数。
    * @param link_url 营销位跳转地址，点击营销位头图跳到的链接url。营销位需要展示头图时，必须填写该参数。
    * @param link_type 跳转类型，网页:HTTP、小程序:APP，不传默认HTTP
    * @param topic_items 营销位内容，数量限制：大于4个，小于8个
    * @return boolean
    * @throws AlipayException 
     */
    public boolean modify(String topic_id,String title,String sub_title,String img_url,
            String link_url,String link_type,List<TopicItem> topic_items) throws AlipayException {
        if (ObjectHelper.isEmpty(topic_id)) {
            throw new IllegalArgumentException("topic_id can't be null or empty");
        }
        if (ObjectHelper.isEmpty(title)) {
            throw new IllegalArgumentException("title can't be null or empty");
        }
        if (ObjectHelper.isEmpty(topic_items)) {
            throw new IllegalArgumentException("topic_items can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("topic_id", topic_id);
        body.put("title", title);
        body.put("sub_title", sub_title);
        body.put("img_url", img_url);
        body.put("link_url", link_url);
        body.put("link_type", link_type);
        body.put("topic_items", JSONObject.toJSONString(topic_items));
        body.put("method", "alipay.open.public.topic.modify");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_topic_modify_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_topic_modify_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    /**
    * @Title: 营销位删除接口 
    * @Description: 通过此接口,开发者可以根据营销位的id删除指定的营销位。
    * @author heliang
    * @param topic_id  营销位id
    * @return boolean
    * @throws AlipayException 
     */
    public boolean delete(String topic_id) throws AlipayException {
        if (ObjectHelper.isEmpty(topic_id)) {
            throw new IllegalArgumentException("topic_id can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("topic_id", topic_id);
        body.put("method", "alipay.open.public.topic.delete");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_topic_delete_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_topic_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    /**
    * @Title: 营销位查询接口 
    * @Description: 通过此接口,开发者可以查询已经在生活号主页配置的营销位具体信息，包括标题、描述、链接等信息。
    * @author heliang
    * @return List<Topic>
    * @throws AlipayException 
     */
    public List<Topic> batchquery() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.topic.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_topic_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_topic_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<Topic> topics = new ArrayList<Topic>();
        JSONArray array = bodyRes.getJSONArray("topic_list");
        for(int i=0;i<array.size();i++) {
            Topic topic = JSONObject.toJavaObject(array.getJSONArray(i), Topic.class);
            topics.add(topic);
        }
        return topics;
    }
}
